package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.extend.utils.LogUtil;
import com.ys.web.dao.CommonDao;
import com.ys.web.entity.OriginBaidu;
import com.ys.web.entity.Originality;
import com.ys.web.pojo.CompanyInfo;
import com.ys.web.pojo.MaterialCreateInfo;
import com.ys.web.service.MaterialCreateService;
import com.ys.web.service.OriginBaiduService;
import com.ys.web.ws.service.ManageCompanyInfoService;

@Service
@Transactional
public class MaterialCreateServiceImpl implements MaterialCreateService{

	
	private String[] promoteUnits = new String[]{"核心词","拓展词","高频词"};
	
	@Autowired
	private CommonDao commonDao;
	
	@Autowired
	private ManageCompanyInfoService manageCompanyInfoService; 
	
	
	@Override
	public List<MaterialCreateInfo> findMaterialCreateInfo(String industryId, String mainBussiness, int page) {
		// TODO Auto-generated method stub
		Map<String,List<OriginBaidu>> map = new HashMap<String,List<OriginBaidu>>();
		List<MaterialCreateInfo> materialCreateInfos = new ArrayList<MaterialCreateInfo>();
		List<CompanyInfo> companyInfos = manageCompanyInfoService.getCompanyByIndustry_ws(industryId);
		try {
		MaterialCreateInfo materialCreateInfoPlan = new MaterialCreateInfo();//推广计划
		materialCreateInfoPlan.setPromotionPlanName(mainBussiness);
		materialCreateInfos.add(materialCreateInfoPlan);
		
		//当存在行业所在的公司时才返回查询结果
		if(companyInfos.size()>0){
		StringBuffer industryBuffer= new StringBuffer("and (1=0");
		for(CompanyInfo companyInfo:companyInfos){
			industryBuffer.append(" or companyId ="+companyInfo.getId());
		}
		industryBuffer.append(")");
		//核心词获取
		Query query = commonDao.getSession().createQuery("from OriginBaidu where LENGTH(keywordSearch)<5 and keywordSearch like ? "+industryBuffer.toString());
		query.setParameter(0,'%' + mainBussiness + '%');
		query.setFirstResult(page);
		query.setMaxResults(100);
		List<OriginBaidu> originBaidusCore= query.list();
		LogUtil.info(originBaidusCore);
		if(originBaidusCore!=null&&originBaidusCore.size()>0){
			
			MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
			materialCreateInfoUnit.setPromotionUnitName(promoteUnits[0]);
			materialCreateInfos.add(materialCreateInfoUnit);
			originBaidusCore.forEach(p->{
				MaterialCreateInfo keywordMaterialCreateInfo =  new MaterialCreateInfo();
				
				keywordMaterialCreateInfo.setKeyword(p.getKeywordSearch());
				materialCreateInfos.add(keywordMaterialCreateInfo);
				
			});
		}
		
		//拓展词获取
		query = commonDao.getSession().createQuery("from OriginBaidu where LENGTH(keywordSearch)>5 and keywordSearch like ? "+industryBuffer.toString());
		query.setParameter(0,'%' + mainBussiness + '%');
		query.setFirstResult(page);
		query.setMaxResults(100);
		List<OriginBaidu> originBaidusExtract= query.list();
		if(originBaidusExtract!=null&&originBaidusExtract.size()>0){
			
			MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
			materialCreateInfoUnit.setPromotionUnitName(promoteUnits[1]);
			materialCreateInfos.add(materialCreateInfoUnit);
			originBaidusExtract.forEach(p->{
				MaterialCreateInfo keywordMaterialCreateInfo =  new MaterialCreateInfo();
				
				keywordMaterialCreateInfo.setKeyword(p.getKeywordSearch());
				materialCreateInfos.add(keywordMaterialCreateInfo);
				
			});
		}
		
		
		//高频词获取
		query = commonDao.getSession().createQuery("from OriginBaidu where keywordSearch like ? "+industryBuffer.toString()+"  order by numberClick desc ");
		query.setParameter(0,'%' + mainBussiness + '%');
		query.setFirstResult(page);
		query.setMaxResults(100);
		List<OriginBaidu> originBaidusHighSequence= query.list();
		if(originBaidusHighSequence!=null&&originBaidusHighSequence.size()>0){
					
					MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
					materialCreateInfoUnit.setPromotionUnitName(promoteUnits[2]);
					materialCreateInfos.add(materialCreateInfoUnit);
					originBaidusHighSequence.forEach(p->{
						MaterialCreateInfo keywordMaterialCreateInfo =  new MaterialCreateInfo();
						
						keywordMaterialCreateInfo.setKeyword(p.getKeywordSearch());
						materialCreateInfos.add(keywordMaterialCreateInfo);
					});
		}
		//获取创意
		query = commonDao.getSession().createQuery("from Originality where  originalityDescribeOne like ? "+industryBuffer.toString()+" order by  numberClick desc,clickRate desc ");	
		query.setParameter(0,'%' + mainBussiness + '%');
		query.setFirstResult(page);
		query.setMaxResults(100);
		List<Originality> originalities= query.list();
		if(originalities!=null){
			
//			MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
//			materialCreateInfoUnit.setPromotionPlanName(mainBussiness);
//			
//			materialCreateInfos.add(materialCreateInfoUnit);
			originalities.forEach(p->{
				MaterialCreateInfo materialCreateInfo = new MaterialCreateInfo();
				
				materialCreateInfo.setOriginalityTitle(p.getOriginalityTitle());
				materialCreateInfo.setOriginalityDescribeOne(p.getOriginalityDescribeOne());
				materialCreateInfo.setOriginalityDescribeTwo(p.getOriginalityDescribeTwo());
				materialCreateInfo.setOriginalityDisplayURL(p.getDisplayURL());
				materialCreateInfos.add(materialCreateInfo);
			});
		}
		
		
		
		}
		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return materialCreateInfos;
	}


	@Override
	public List<MaterialCreateInfo> findMaterialCreateInfoForCsv(String industryId, String mainBussiness, int page) {
		// TODO Auto-generated method stub
				Map<String,List<OriginBaidu>> map = new HashMap<String,List<OriginBaidu>>();
				List<MaterialCreateInfo> materialCreateInfos = new ArrayList<MaterialCreateInfo>();
				List<CompanyInfo> companyInfos = manageCompanyInfoService.getCompanyByIndustry_ws(industryId);
				try {
				MaterialCreateInfo materialCreateInfoPlan = new MaterialCreateInfo();//推广计划
				materialCreateInfoPlan.setPromotionPlanName(mainBussiness);
				materialCreateInfos.add(materialCreateInfoPlan);
				
				//当存在行业所在的公司时才返回查询结果
				if(companyInfos.size()>0){
				StringBuffer industryBuffer= new StringBuffer("and (1=0");
				for(CompanyInfo companyInfo:companyInfos){
					industryBuffer.append(" or companyId ="+companyInfo.getId());
				}
				industryBuffer.append(")");
				//核心词获取
				Query query = commonDao.getSession().createQuery("from OriginBaidu where LENGTH(keywordSearch)<5 and keywordSearch like ? "+industryBuffer.toString());
				query.setParameter(0,'%' + mainBussiness + '%');
				query.setFirstResult(page);
				query.setMaxResults(100);
				List<OriginBaidu> originBaidusCore= query.list();
				LogUtil.info(originBaidusCore);
				if(originBaidusCore!=null&&originBaidusCore.size()>0){
					
					MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
					materialCreateInfoUnit.setPromotionUnitName(promoteUnits[0]);
					materialCreateInfos.add(materialCreateInfoUnit);
					originBaidusCore.forEach(p->{
						MaterialCreateInfo keywordMaterialCreateInfo =  new MaterialCreateInfo();
						
						keywordMaterialCreateInfo.setKeyword(p.getKeywordSearch());
						materialCreateInfos.add(keywordMaterialCreateInfo);
						
					});
				}
				
				//拓展词获取
				query = commonDao.getSession().createQuery("from OriginBaidu where LENGTH(keywordSearch)>5 and keywordSearch like ? "+industryBuffer.toString());
				query.setParameter(0,'%' + mainBussiness + '%');
				query.setFirstResult(page);
				query.setMaxResults(100);
				List<OriginBaidu> originBaidusExtract= query.list();
				if(originBaidusExtract!=null&&originBaidusExtract.size()>0){
					
					MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
					materialCreateInfoUnit.setPromotionUnitName(promoteUnits[1]);
					materialCreateInfoUnit.setPromotionPlanState("有效");
					materialCreateInfoUnit.setPromotionPlanEnable("启动");
					materialCreateInfoUnit.setPromotionUnitState("有效");
					materialCreateInfoUnit.setPromotionUnitEnable("启用");
					materialCreateInfos.add(materialCreateInfoUnit);
					originBaidusExtract.forEach(p->{
						MaterialCreateInfo keywordMaterialCreateInfo =  new MaterialCreateInfo();
						keywordMaterialCreateInfo .setPromotionPlanName(p.getPromotionPlan());
						keywordMaterialCreateInfo .setPromotionUnitName(p.getPromotionUnit());
						keywordMaterialCreateInfo .setPromotionPlanState("有效");
						keywordMaterialCreateInfo .setPromotionPlanEnable("启动");;
						keywordMaterialCreateInfo .setPromotionUnitState("有效");
						keywordMaterialCreateInfo .setPromotionUnitEnable("启用");
						keywordMaterialCreateInfo.setKeyword(p.getKeywordSearch());
						materialCreateInfos.add(keywordMaterialCreateInfo);
						
					});
				}
				
				
				//高频词获取
				query = commonDao.getSession().createQuery("from OriginBaidu where keywordSearch like ? "+industryBuffer.toString()+"  order by numberClick desc ");
				query.setParameter(0,'%' + mainBussiness + '%');
				query.setFirstResult(page);
				query.setMaxResults(100);
				List<OriginBaidu> originBaidusHighSequence= query.list();
				if(originBaidusHighSequence!=null&&originBaidusHighSequence.size()>0){
							
							MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
							materialCreateInfoUnit.setPromotionUnitName(promoteUnits[2]);
							materialCreateInfoUnit.setPromotionPlanState("有效");
							materialCreateInfoUnit.setPromotionPlanEnable("启动");
							materialCreateInfoUnit.setPromotionUnitState("有效");
							materialCreateInfoUnit.setPromotionUnitEnable("启用");
							materialCreateInfos.add(materialCreateInfoUnit);
							originBaidusHighSequence.forEach(p->{
								MaterialCreateInfo keywordMaterialCreateInfo =  new MaterialCreateInfo();
								
								keywordMaterialCreateInfo.setKeyword(p.getKeywordSearch());
								materialCreateInfos.add(keywordMaterialCreateInfo);
							});
				}
				//获取创意
				query = commonDao.getSession().createQuery("from Originality where  originalityDescribeOne like ? "+industryBuffer.toString()+" order by  numberClick desc,clickRate desc ");	
				query.setParameter(0,'%' + mainBussiness + '%');
				query.setFirstResult(page);
				query.setMaxResults(100);
				List<Originality> originalities= query.list();
				if(originalities!=null){
					
//					MaterialCreateInfo materialCreateInfoUnit = new MaterialCreateInfo();//推广单元
//					materialCreateInfoUnit.setPromotionPlanName(mainBussiness);
//					
//					materialCreateInfos.add(materialCreateInfoUnit);
					originalities.forEach(p->{
						MaterialCreateInfo materialCreateInfo = new MaterialCreateInfo();
						materialCreateInfo.setPromotionPlanName(p.getPromotionPlan());
						materialCreateInfo.setPromotionUnitName(p.getPromotionUnit());
						materialCreateInfo.setPromotionPlanState("有效");
						materialCreateInfo.setPromotionPlanEnable("启动");;
						materialCreateInfo.setPromotionUnitState("有效");
						materialCreateInfo.setPromotionUnitEnable("启用");
						materialCreateInfo.setOriginalityTitle(p.getOriginalityTitle());
						materialCreateInfo.setOriginalityDescribeOne(p.getOriginalityDescribeOne());
						materialCreateInfo.setOriginalityDescribeTwo(p.getOriginalityDescribeTwo());
						materialCreateInfo.setOriginalityDisplayURL(p.getDisplayURL());
						materialCreateInfos.add(materialCreateInfo);
					});
				}
				
				
				
				}
				
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				
				return materialCreateInfos;
	}

}
